from datetime import datetime
from flask import render_template, session, redirect, url_for

from . import  main
from .forms import NameForm
from .. import db
from ..models import User

# routes
@main.route('/', methods=['POST', 'GET'])
def index():
    form = NameForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.name.data).first()
        if user is None:
            user = User(username=form.name.data)
            db.session.add(user)
            session['known'] = False
            #if app.config['FLASK_ADMIN']:
                #send_mail(app.config['FLASK_ADMIN'], 'New User',
                         # 'email/new_user', user=user)
        else:
            session['known'] = True

        session['name'] = form.name.data
        form.name.data = ''
        return redirect(url_for('.index'))
    return render_template('index.html',
                           form=form,
                           name=session.get('name'),
                           known=session.get('known', False))


@main.route('/user/<name>')
def user(name):
    return render_template('user.html', name=name)